package com.houtai.controller.problem;


import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.RetInfo;
import com.houtai.base.WangEditor;
import com.houtai.bean.TsJzAdmin;
import com.houtai.bean.TsProblem;
import com.houtai.bean.dto.problem.ProblemListDto;
import com.houtai.common.Contant;
import com.houtai.common.oss.OssUtil;
import com.houtai.service.funcion.FuncionService;
import com.houtai.service.service.ProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by lizhun on 2017/7/19.
 */
@Controller
@RequestMapping(value = {"v5"})
public class ProblemController {
    @Resource
    private ProblemService problemService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private FuncionService funcionService;

    /**
     * @Purpose  分页加载常见问题
     * @version  1.0
     * @author   lizhun
     * @param    request
     * @return   RetInfo
     */
    @RequestMapping(value="/problem/list")
    @Authority(AuthorityType.Login)
    public ModelAndView list(HttpServletRequest request, ProblemListDto page){
        ModelAndView mv = new ModelAndView("problem/list");
        TsJzAdmin tsJzAdmin = (TsJzAdmin) redisTemplate.opsForValue().get(request.getParameter("token")) ;
        mv.addObject("retInfo", problemService.findSchoolPage(page, request));
        mv.addObject("token", request.getParameter("token"));
        mv.addObject("father_id", request.getParameter("father_id"));
        mv.addObject("funcions", funcionService.findSonFuncion(tsJzAdmin, Integer.valueOf(request.getParameter("father_id"))));
        return mv;
    }
    /**
     * @Purpose  跳转到添加常见问题页面
     * @version  1.0
     * @author   lizhun
     * @param    request
     * @return   RetInfo
     */
    @RequestMapping(value="/problem/to/add")
    @Authority(AuthorityType.Login)
    public ModelAndView index(HttpServletRequest request){
        ModelAndView mv = new ModelAndView("problem/add");
        mv.addObject("token", request.getParameter("token"));
        return mv;
    }

    /**
     *上传banner图片
     * @param frontFile
     * @return
     */
    @RequestMapping(value = "/problem/fileImg" )
    @ResponseBody
    public WangEditor fileImg(@RequestParam(value = "frontFile", required = true) MultipartFile frontFile)throws Exception{
        Map<String, String> map = new HashMap<>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmssS", Locale.CHINA);

        String frontFileName = frontFile.getOriginalFilename().substring(frontFile.getOriginalFilename().lastIndexOf(".") + 1);
        String imgName = formatter.format(new Date(System.currentTimeMillis()));
        String frontSavePath = Contant.FILE_IMG+"/" + "problem/" + imgName + "." + frontFileName;
        ObjectMetadata frontMetadata = new ObjectMetadata();

        InputStream inputStream = frontFile.getInputStream();
        frontMetadata.setContentLength(inputStream.available());
        OssUtil.uploadInputStream(inputStream, frontMetadata, frontSavePath);

        String[] str = { Contant.DOMAIN +  Contant.FILE_IMG+"/"+"problem"+"/"+imgName + "." + frontFileName};
        WangEditor we = new WangEditor(str);
        return we;
    }
    /**
     * @Purpose   添加常见问题
     * @param     tdProblem
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    @RequestMapping(value = "/problem/add", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    @Authority(AuthorityType.Login)
    public RetInfo insert(@Valid TsProblem tdProblem, BindingResult result, HttpServletRequest request) {
        RetInfo retInfo = new RetInfo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = problemService.insertTsProblem(tdProblem, request);
        }
        return retInfo;
    }
    /**
     * @Purpose  跳转到更新常见问题页面
     * @version  1.0
     * @author   lizhun
     * @param    request
     * @return   RetInfo
     */
    @RequestMapping(value="/problem/to/update")
    @Authority(AuthorityType.Login)
    public ModelAndView index(HttpServletRequest request, Integer p_id){
        ModelAndView mv = new ModelAndView("problem/update");
        TsProblem tdProblem = problemService.findTsProblemById(p_id);
        mv.addObject("token", request.getParameter("token"));
        mv.addObject("problem", tdProblem);
        return mv;
    }
    /**
     * @Purpose   更新常见问题
     * @param     tsProblem
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    @RequestMapping(value = "/problem/update", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    @Authority(AuthorityType.Login)
    public RetInfo update(@Valid  TsProblem tsProblem, BindingResult result, HttpServletRequest request) {
        RetInfo retInfo = new RetInfo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = problemService.updateTsProblem(tsProblem, request);
        }
        return retInfo;
    }
    /**
     * @Purpose   更新常见问题状态
     * @param     p_id
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    @RequestMapping(value = "/problem/update/state", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    @Authority(AuthorityType.Login)
    public RetInfo valid(Integer p_id,  HttpServletRequest request) {

        return problemService.deleteTsProblem(p_id, request);
    }
    /**
     * @Purpose  跳转到更新联系客服页面
     * @version  1.0
     * @author   lizhun
     * @param    request
     * @return   RetInfo
     */
    @RequestMapping(value="/problem/to/contact")
    @Authority(AuthorityType.Login)
    public ModelAndView contact(HttpServletRequest request){
        ModelAndView mv = new ModelAndView("problem/contact");
        TsProblem tdProblem = problemService.findTsProblemById(1);
        mv.addObject("token", request.getParameter("token"));
        mv.addObject("problem", tdProblem);
        return mv;
    }
    /**
     * @Purpose   更新联系客服
     * @param     arti_content
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    @RequestMapping(value = "/problem/update/contact", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    @Authority(AuthorityType.Login)
    public RetInfo update(String arti_content, HttpServletRequest request) {

        return problemService.updateContant(arti_content, request);
    }
}
